import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

/// 通用卡片组件
/// 提供统一的卡片样式，支持主题切换
class CommonCard extends StatelessWidget {
  /// 卡片内容
  final Widget child;

  /// 卡片内边距
  final EdgeInsetsGeometry? padding;

  /// 卡片外边距
  final EdgeInsetsGeometry? margin;

  /// 点击事件
  final VoidCallback? onTap;

  /// 是否显示阴影
  final bool showShadow;

  /// 圆角大小
  final double borderRadius;

  /// 背景颜色
  final Color? backgroundColor;

  const CommonCard({
    super.key,
    required this.child,
    this.padding,
    this.margin,
    this.onTap,
    this.showShadow = true,
    this.borderRadius = 12.0,
    this.backgroundColor,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: margin ?? EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
      decoration: BoxDecoration(
        color: backgroundColor ?? context.theme.cardColor,
        borderRadius: BorderRadius.circular(borderRadius.r),
        boxShadow: showShadow
            ? [
                BoxShadow(
                  color: Colors.black.withOpacity(0.05),
                  blurRadius: 8.r,
                  offset: Offset(0, 2.h),
                ),
              ]
            : null,
      ),
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: onTap,
          borderRadius: BorderRadius.circular(borderRadius.r),
          child: Padding(
            padding: padding ?? EdgeInsets.all(16.w),
            child: child,
          ),
        ),
      ),
    );
  }
}

/// 统计卡片组件
/// 用于显示数值统计信息
class StatCard extends StatelessWidget {
  /// 标题
  final String title;

  /// 数值
  final String value;

  /// 图标
  final IconData icon;

  /// 主题色
  final Color color;

  /// 点击事件
  final VoidCallback? onTap;

  /// 副标题
  final String? subtitle;

  const StatCard({
    super.key,
    required this.title,
    required this.value,
    required this.icon,
    required this.color,
    this.onTap,
    this.subtitle,
  });

  @override
  Widget build(BuildContext context) {
    return CommonCard(
      onTap: onTap,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                padding: EdgeInsets.all(8.w),
                decoration: BoxDecoration(
                  color: color.withOpacity(0.1),
                  borderRadius: BorderRadius.circular(8.r),
                ),
                child: Icon(
                  icon,
                  color: color,
                  size: 20.w,
                ),
              ),
              SizedBox(width: 12.w),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      title,
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: Theme.of(context).textTheme.bodyMedium?.color,
                      ),
                    ),
                    if (subtitle != null) ...[
                      SizedBox(height: 2.h),
                      Text(
                        subtitle!,
                        style: TextStyle(
                          fontSize: 12.sp,
                          color: context.theme.textTheme.bodySmall?.color,
                        ),
                      ),
                    ],
                  ],
                ),
              ),
            ],
          ),
          SizedBox(height: 12.h),
          Text(
            value,
            style: TextStyle(
              fontSize: 24.sp,
              fontWeight: FontWeight.bold,
              color: color,
            ),
          ),
        ],
      ),
    );
  }
}

/// 信息卡片组件
/// 用于显示详细信息
class InfoCard extends StatelessWidget {
  /// 标题
  final String title;

  /// 内容列表
  final List<InfoItem> items;

  /// 点击事件
  final VoidCallback? onTap;

  /// 是否显示箭头
  final bool showArrow;

  const InfoCard({
    super.key,
    required this.title,
    required this.items,
    this.onTap,
    this.showArrow = true,
  });

  @override
  Widget build(BuildContext context) {
    return CommonCard(
      onTap: onTap,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Expanded(
                child: Text(
                  title,
                  style: TextStyle(
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w600,
                    color: context.theme.textTheme.titleMedium?.color,
                  ),
                ),
              ),
              if (showArrow && onTap != null)
                Icon(
                  Icons.arrow_forward_ios,
                  size: 16.w,
                  color: context.theme.textTheme.bodySmall?.color,
                ),
            ],
          ),
          SizedBox(height: 12.h),
          ...items.map((item) => Padding(
                padding: EdgeInsets.only(bottom: 8.h),
                child: Row(
                  children: [
                    Expanded(
                      flex: 2,
                      child: Text(
                        item.label,
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: context.theme.textTheme.bodyMedium?.color,
                        ),
                      ),
                    ),
                    Expanded(
                      flex: 3,
                      child: Text(
                        item.value,
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: context.theme.textTheme.titleMedium?.color,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ),
                  ],
                ),
              )),
        ],
      ),
    );
  }
}

/// 信息项模型
class InfoItem {
  final String label;
  final String value;

  InfoItem({
    required this.label,
    required this.value,
  });
}
